package com.mall.shopping.services;

import com.alibaba.druid.util.StringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mall.commons.tool.exception.ValidateException;
import com.mall.shopping.IProductService;
import com.mall.shopping.constants.ShoppingRetCode;
import com.mall.shopping.converter.ProductConverter;
import com.mall.shopping.dal.entitys.Item;
import com.mall.shopping.dal.entitys.ItemCat;
import com.mall.shopping.dal.entitys.ItemDesc;
import com.mall.shopping.dal.persistence.ItemCatMapper;
import com.mall.shopping.dal.persistence.ItemDescMapper;
import com.mall.shopping.dal.persistence.ItemMapper;
import com.mall.shopping.dto.*;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

@Service
public class ProductServiceImpl implements IProductService {

    @Autowired
    ItemMapper itemMapper;

    @Autowired
    ProductConverter productConverter;
    @Autowired
    ItemDescMapper itemDescMapper;

    // 通过商品id查询商品信息
    @Override
    public ProductDTOResponse getProductDTO(ProductDTORequest request) {

        ProductDTOResponse productDTOResponse = new ProductDTOResponse();

        //校验
        try {
            request.requestCheck();
        } catch (ValidateException exception) {
            exception.printStackTrace();

            productDTOResponse.setCode(exception.getErrorCode());
            productDTOResponse.setMsg(exception.getMessage());
            return productDTOResponse;
        }
        Item item = itemMapper.selectByPrimaryKey(request.getId());

        // 判断非空

        // 赋值
        productDTOResponse.setTitle(item.getTitle());
        productDTOResponse.setPrice(item.getPrice());

        productDTOResponse.setCode(ShoppingRetCode.SUCCESS.getCode());
        productDTOResponse.setMsg(ShoppingRetCode.SUCCESS.getMessage());

        return productDTOResponse;
    }

    @Override
    public ProductDetailDto getProductDetailDTO(ProductDTORequest productDTORequest) {
        Long id = productDTORequest.getId();
        Item item = itemMapper.selectByPrimaryKey(id);
        ProductDetailDto productDetailDto = new ProductDetailDto();
        productDetailDto.setProductId(id);
        productDetailDto.setSalePrice(item.getPrice());
        productDetailDto.setProductName(item.getTitle());
        productDetailDto.setLimitNum(Long.valueOf(item.getLimitNum()));
        productDetailDto.setProductImageBig(item.getImage());
        ArrayList<String> strings = new ArrayList<>();
        String[] images = item.getImages();
        for (String image : images) {
            strings.add(image);
        }
        productDetailDto.setProductImageSmall(strings);
        ItemDesc itemDesc = itemDescMapper.selectByPrimaryKey(id);
        productDetailDto.setDetail(itemDesc.getItemDesc());
        productDetailDto.setSubTitle(item.getSellPoint());
        return productDetailDto;
    }

    @Override
    public AllProductResponse getGoods(Integer page, Integer size, String sort, Long cid, Integer priceGt, Integer priceLte) {
        PageHelper.startPage(page, size);
        AllProductResponse allProductResponse = new AllProductResponse();
        Example example = new Example(Item.class);
        if(priceGt!=null||priceLte!=null){
            if (priceGt != null&&priceLte==null) {
                example.createCriteria().andGreaterThanOrEqualTo("price", priceGt);
            }
            if (priceLte != null&&priceGt==null) {
                example.createCriteria().andLessThanOrEqualTo("price", priceLte);
            }
            example.createCriteria().andBetween("price",priceGt,priceLte);
        }
        if (cid != null) {
            example.createCriteria().andEqualTo("cid", cid);
        }
        if (!StringUtils.isEmpty(sort)) {
            if (sort.equals("-1")) {
                example.orderBy("price").desc();
            } else {
                example.orderBy("price").asc();
            }
        }
        List<Item> itemList = itemMapper.selectByExample(example);
        PageInfo<Item> itemPageInfo = new PageInfo<>(itemList);
        long total = itemPageInfo.getTotal();
        List<ProductDto> productDto = productConverter.items2Dto(itemList);
        allProductResponse.setProductDtoList(productDto);
        allProductResponse.setTotal(total);
        return allProductResponse;
    }
}
